/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package anivis.ui.timeline;

import anivis.layers.ObservationLayer;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataShapeAction;
import prefuse.action.layout.AxisLayout;
import prefuse.controls.ToolTipControl;
import prefuse.data.Table;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.ShapeRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;
import prefuse.visual.expression.VisiblePredicate;

/**
 *
 * @author urp
 */
public class ObservationScatterPlot extends Display
{
    private static final String group = "data";
    
    private ShapeRenderer m_shapeR = new ShapeRenderer(2);
    
    public ObservationScatterPlot(ObservationLayer layer, String xfield, String yfield) 
    {
        this(layer, xfield, yfield, null);
    }
    
    public ObservationScatterPlot(ObservationLayer layer, String xfield, String yfield, String sfield) 
    {
        super(new Visualization());
        
        // --------------------------------------------------------------------
        // STEP 1: setup the visualized data
        
        m_vis.addTable(group, layer.getTable());
        
        DefaultRendererFactory rf = new DefaultRendererFactory(m_shapeR);
        m_vis.setRendererFactory(rf);
        
        // --------------------------------------------------------------------
        // STEP 2: create actions to process the visual data
        
        // set up the actions
        AxisLayout x_axis = new AxisLayout(group, xfield, 
                Constants.X_AXIS, VisiblePredicate.TRUE);
        m_vis.putAction("x", x_axis);
        
        AxisLayout y_axis = new AxisLayout(group, yfield, 
                Constants.Y_AXIS, VisiblePredicate.TRUE);
        m_vis.putAction("y", y_axis);

        ColorAction color = new ColorAction(group, 
                VisualItem.STROKECOLOR, ColorLib.rgb(100,100,255));
        m_vis.putAction("color", color);
        
        DataShapeAction shape = new DataShapeAction(group, sfield);
        m_vis.putAction("shape", shape);
        
        ActionList draw = new ActionList();
        draw.add(x_axis);
        draw.add(y_axis);
        if ( sfield != null )
            draw.add(shape);
        draw.add(color);
        draw.add(new RepaintAction());
        m_vis.putAction("draw", draw);
        
        // --------------------------------------------------------------------
        // STEP 3: set up a display and ui components to show the visualization

        setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
        setSize(700,450);
        setHighQuality(true);
        
        ToolTipControl ttc = new ToolTipControl(new String[] {xfield,yfield});
        addControlListener(ttc);
        
        
        // --------------------------------------------------------------------        
        // STEP 4: launching the visualization
                
        m_vis.run("draw");
    }  
    
    public void setLayer(ObservationLayer newLayer)
    {
       m_vis.removeGroup(group);
       m_vis.addTable(group, newLayer.getTable());
       
    }        
    
    public int getPointSize() {
        return m_shapeR.getBaseSize();
    }
    
    public void setPointSize(int size) {
        m_shapeR.setBaseSize(size);
        repaint();
    }
}
